﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.ComponentModel;
using System.Text;
using System.Reflection;
using log4net;

/// <summary>
/// Hỗ trợ, tối ưu các câu lệnh LINQ
/// </summary>
public class LINQUtil
{
    public LINQUtil()
	{
	}

    /// <summary>
    /// Dùng để Select (không gọi db.SubmitChange())
    /// </summary>
    /// <returns></returns>
    public static DBDataContext GetDBReadOnly()
    {
        DBDataContext db = new DBDataContext();
        db.ObjectTrackingEnabled = false;
        return db;
    }

    /// <summary>
    /// Dùng để Insert, Update, Delete (có gọi db.SubmitChange())
    /// </summary>
    /// <returns></returns>
    public static DBDataContext GetDBWriteOnly()
    {
        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        DBDataContext db = new DBDataContext(conn);
        return db;
    }

    /// <summary>
    /// Random một List
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <returns></returns>
    public static List<T> OrderRandom<T>(List<T> list) {
        return list.OrderBy(e => Guid.NewGuid()).ToList();
    }
   

}
public partial class DBDataContext{
    protected ILog log = LogManager.GetLogger(typeof(DBDataContext));
    public void Delete<T>(T obj) where T : class
    {
        try
        {
            Table<T> t = this.GetTable<T>();
            string tableName = typeof(T).Name;
          //  log.Info("Delete Success" + tableName);
            t.DeleteOnSubmit(obj);
        }
        catch(Exception ex) {
            log.Error("--------------------------- ");
            log.Error("Delete Error: " + ex.Message);
            log.Error("--------------------------- ");
        }
    }

    public void Delete<T>(List<T> objs) where T : class
    {
        try
        {
            Table<T> t = this.GetTable<T>();
            string tableName = typeof(T).Name;
           // log.Info("Delete Success" + objs.Count + " " + tableName);
            t.DeleteAllOnSubmit(objs);
           // CacheUtil.ClearAllCache();
        }
        catch (Exception ex)
        {
            log.Error("--------------------------- ");
            log.Error("Delete Error: " + ex.Message);
            log.Error("--------------------------- ");
        }
    }
    public void Insert<T>(T obj) where T : class
    {
        string tableName = "";
        try
        {
            Table<T> t = this.GetTable<T>();
            tableName = typeof(T).Name;
           // log.Info("Insert Success" + tableName);
            t.InsertOnSubmit(obj);
            //CacheUtil.ClearAllCache();
        }
        catch (Exception ex)
        {
            log.Error("--------------------------- ");
            log.Error("Insert " + tableName + " Error: " + ex.Message);
            log.Error("--------------------------- ");
        }
    }


    public void Insert<T>(List<T> objs) where T : class
    {
        string tableName = "";
        try
        {
            Table<T> t = this.GetTable<T>();
            tableName = typeof(T).Name;
        //    log.Info("Insert Success" + objs.Count + " " + tableName);
            t.InsertAllOnSubmit(objs);
            CacheUtil.ClearAllCache();
        }
        catch (Exception ex)
        {
            log.Error("--------------------------- ");
            log.Error("Insert " + tableName + " Error: " + ex.Message);
            log.Error("--------------------------- ");
        }
    }
    public void Update<T>(T obj) where T : class
    {
        string tableName = "";
        try
        {
            Table<T> t = this.GetTable<T>();
             tableName = typeof(T).Name;
           // log.Info("Update Success" + tableName);
        }
        catch (Exception ex)
        {
            log.Error("--------------------------- ");
            log.Error("Update " + tableName + " Error: " + ex.Message);
            log.Error("--------------------------- ");
        }
    }

    public void Update<T>(List<T> objs) where T : class
    {
        string tableName = "";
        try
        {
            Table<T> t = this.GetTable<T>();
             tableName = typeof(T).Name;
            //log.Info("Update Success" + objs.Count + " " + tableName);
        }
        catch (Exception ex)
        {
            log.Error("--------------------------- ");
            log.Error("Update " + tableName + " Error: " + ex.Message);
            log.Error("--------------------------- ");
        }
        
    }
    
}
